/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ac.persistence;

import ac.entities.TanqueAnimal;
import com.ibatis.sqlmap.client.SqlMapClient;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * Controla la persistencia de la relación entre Tanques y Animales
 * @author prometheus
 */
public class PersistTanqueAnimal {

    public boolean insertTanqueAnimal (TanqueAnimal ta) {
        boolean result = false;
        try {
            SqlMapClient sqlMap = SqlConnector.getInstance().getReady();
            sqlMap.insert("insertTanqueAnimal", ta);
            result = true;
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }

    public boolean updateTanqueAnimal (int id, Date fechaFin) {
        boolean result = false;
        HashMap params = new HashMap();
        params.put("id", id);
        params.put("fechaFin", fechaFin);
        System.out.println("id = " + id + " fechaFin = " + fechaFin);
        try {
            SqlMapClient sqlMap = SqlConnector.getInstance().getReady();
            sqlMap.update("updateTanqueAnimal", params);
            result = true;
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }

    public ArrayList<TanqueAnimal> buscarTodosPorAnimal (int id) {
        ArrayList<TanqueAnimal> result = null;
        try {
            SqlMapClient sqlMap = SqlConnector.getInstance().getReady();
            result = (ArrayList) sqlMap.queryForList("buscarTodosPorAnimal", id);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }

    public int buscarUltimoTanqueAnimal () {
        int result = 0;
        try {
            SqlMapClient sqlMap = SqlConnector.getInstance().getReady();
            result = (Integer) sqlMap.queryForObject("ultimoIdTanAni");
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }

    public TanqueAnimal buscarTanqueAnimalActual (int idAnimal) {
        TanqueAnimal result = null;
        try {
            SqlMapClient sqlMap = SqlConnector.getInstance().getReady();
            result = (TanqueAnimal) sqlMap.queryForObject("buscarTanqueAnimalActual", idAnimal);
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (SQLException sqle) {
            sqle.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return result;
        }
    }
}
